
const BASE_URL = "https://api.douban.com/v2/movie"
function LoginTKDES() {
	
 }
/**
 * 方法调用
 */
export const getInTheaters = params => (
	new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_URL}/in_theaters`,
			data: params,
			header: {
				"content-type": "json"
			},
			success: (res) => {
				resolve(res);
			}

		});
	})
)


/**
 * 封装get请求
 */
export const getRequest = (url, params, showLoading) => (
	new Promise((resolve, reject) => {
		if (showLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		let token = uni.getStorageSync("token");
		
		if(token==""){
			console.log("tokentokentokentoken:",token)
			uni.hideLoading();
			return
		}
		uni.request({
			url: `${url}`,
			method: "GET",
			data: params,
			header: {
				'tenant-id': 1,
				"content-type": "application/json",
				"Authorization": "Bearer "+token
			},
			success: (res) => {
				//console.log("success："+JSON.stringify(res))
				if (res.statusCode == 200) {
					if(res.data.code == 0)	{
						resolve(res.data)
					} else if(res.data.code == 401){
						uni.navigateTo({
									   url: "/pages/login/login"
						});
					}else{
						uni.$u.toast(res.data.msg)
					}
				
				} else {
					reject("接口程序出错");
					uni.$u.toast("程序异常，请联系管理员！")
				}
				

			},
			fail: (res) => {
				console.log("fail："+JSON.stringify(res))
				reject("网络出错了");
			},
			complete: () => {
				if (showLoading) {
					uni.hideLoading();
				}
			}
		})
	})
)

/**
 * 封装post请求
 */

export const postRequest = (url, params, showLoading) => (
	new Promise((resolve, reject) => {
		if (showLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		
		let token = uni.getStorageSync("token");
		console.log("taken",token)
		uni.request({
			url: `${url}`,
			method: "POST",
			data: params,
			header: {
				'tenant-id': 1,
				"content-type": "application/json",
				"Authorization": "Bearer "+token
			},
			success: (res) => {
				console.log("postRequest:"+JSON.stringify(res))
				
				if (res.statusCode == 200) {
					 if(res.data.code == 0)	{
					 	resolve(res.data)
					 } else if(res.data.code == 401){
					 	uni.showModal({
					 	    title: '提示',
					 	    content: '账号未登录',
					 		showCancel: false,//没有取消按钮的弹框
					 		buttonText: '确定',
					 	    success: function (res) {
					 		uni.navigateTo({
					 				   url: "/pages/login/login"
					 				});
					 			}
					 	});
					 }else{
					 	//uni.$u.toast(res.data.msg)
						//console.log('接口返回');
						uni.showToast({
						 	title:res.data.msg,
						 	icon:"error",
						 	duration: 1500
						 })
						return 
					 }
				} else {
					reject("接口程序出错");
					uni.$u.toast("程序异常，请联系管理员！")
				}
				/* if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					reject("接口程序出错");
					uni.$u.toast("程序异常，请联系管理员！")
				} */
			},
			fail: (res) => {
				reject("连接不上服务器");
				uni.$u.toast("网络异常，请稍后在登录！")
			},
			complete: () => {
				if (showLoading) {
					//console.log('關閉');
					setTimeout(() => {
						uni.hideLoading();
					}, 1500);
				}
			}
		})
	})
)
export const loginRequest = (url, params, showLoading) => (
	new Promise((resolve, reject) => {
		
		if (showLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		uni.request({
			url: `${url}`,
			method: "POST",
			data: params,
			header: {
				'tenant-id': 1,
				"content-type": "application/json"
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					reject("接口程序出错");
					uni.$u.toast("程序异常，请联系管理员！")
				}
			},
			fail: (res) => {
				reject("连接不上服务器");
				uni.$u.toast("网络异常，请稍后在登录！")
			},
			complete: () => {
				if (showLoading) {
					uni.hideLoading();
				}
			}
		})
	})
)
/**
 * 超时
 */
export const getTimeOut = () => (
	new Promise((resolve, reject) =>{
		setTimeout(()=> {
			reject('请求超时');
		}, 50);
	})
)

 module.exports = {
     postRequest: postRequest,
	 loginRequest:loginRequest,
	 getRequest:getRequest,
	 getTimeOut:getTimeOut,
	 getInTheaters:getInTheaters
 }